<?php
/**
 * Semester Tutor   Controller
 * Semester Teacher  Tutor   Management
 *
 * semester entry
 *
 * @version $Id: TutorController.php 88 2010-05-01 13:27:37Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */
class Semester_TutorController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */

    }

    public function indexAction()
    {

        if ($this->getRequest()->isPost() and $this->getRequest()->getParam('act') ){

            $classId = $this->getRequest()->getParam('class_id');
            $yearSemester = $this->getRequest()->getParam('year_semester');
            $tempArr = explode("-",$yearSemester);
            $year = (int)$tempArr[0];
            $semester = (int)$tempArr[1];
            //  先清除任教班級設定再重設
            $q = Doctrine_Query::create()
            ->update('Semester_Model_SemesterTeacher')
            ->set('class_num=','?','')
            ->where('year=?',$year)
            ->andwhere('semester=?', $semester);
            $q->execute();

            foreach($classId as $id => $val) {

                $q = Doctrine_Query::create()
                ->update('Semester_Model_SemesterTeacher')
                ->set('class_num','?',$id)
                ->where('year=?',$year)
                ->andwhere('semester=?', $semester)
                ->andWhere('teacher_id=?',$val);
                $q->execute();
            }
        }

        if (!$this->_request->getParam('year_semester'))
        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        else
        $this->view->year_semester = $this->_request->getParam('year_semester');
        // 班級名稱
        $semester = new Semester_Model_SemesterClass();
        $this->view->class_name = $semester->getGradeClassNames($this->view->year_semester);

        // 教師列表
        $teacher = new Teacher_Model_Teacher();
        $tt = $teacher->getTeachers();
        $this->view->teachers_sn = Zend_Json::encode(array_keys($tt));
        $this->view->teachers = Zend_Json::encode($tt);

        // 導師姓名
        $semesterTeacher = new Semester_Model_SemesterTeacher();
        $this->view->toturs = $semesterTeacher->getTutors($this->view->year_semester);
    }

    function searchAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->getHelper('ViewRenderer')->setNorender();
        $str = $this->getRequest()->getParam('q');
        if (!$str)
        echo '';
        else {
            $f = new Zend_Filter_StripTags();
            $yearSemester = $this->getRequest()->getParam('year_semester');
            $tempArr = explode("-", $yearSemester);
            $sourceYear =  null;
            if (isset( $tepmArr))
            $sourceYear = $tepmArr[0].$tempArr[1];
            $currSemester = false;

            if ($sourceYear ==  (int)$this->view->Semester)
            $currSemester = true;

            $str = $f->filter($str);
            $q = Doctrine_Query::create()
            ->select("username,name")
            ->from('Teacher_Model_TeacherView')
            ->where("name LIKE '%$str%'" );
            if ($currSemester)
            $q->andWhere('condition_state=?', 0 );
            $res = $q->fetchArray();
            foreach($res as $row)
            echo $row['name'].'|'.$row['username']."\n";
        }

    }


}

